//
//  OrderingTeacherController.m
//  SDSY
//
//  Created by 蓝布鲁 on 2017/6/11.
//  Copyright © 2017年 蓝布鲁. All rights reserved.
//

#import "OrderingTeacherController.h"
#import "UserInfoModel.h"
#import "OrderedTeacherModel.h"
#import "EvalutionTeacherController.h"
#import "ViewEvalutionTeacherController.h"
#import "OrderedTeacherCell.h"
#import "TeacherInfoViewController.h"
#import "DOPDropDownMenu.h"

@interface OrderingTeacherController ()<DOPDropDownMenuDataSource,DOPDropDownMenuDelegate,UITableViewDelegate,UITableViewDataSource>

// 视图
@property (nonatomic,retain)DOPDropDownMenu *menu;
@property (nonatomic,retain)UITableView *tableView;

// 数据
@property (nonatomic, retain)NSMutableArray *dataArray;         // 导师模型数据
@property (nonatomic,retain)NSArray *sortArray;                 // 排序数组
@property (nonatomic,assign)NSInteger sort;                     // 排序方式
@property (nonatomic,assign)NSInteger page;

@end

@implementation OrderingTeacherController

-(instancetype)init {
    if (self = [super init]) {
        self.title = @"正在预约的导师";
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.初始化下拉菜单
    self.menu = [[DOPDropDownMenu alloc] initWithOrigin:CGPointMake(0, 64) andHeight:44];
    self.menu.delegate = self;
    self.menu.dataSource = self;
    
    // 网络本地获取下拉数据
    [self updateMenuDate];
    
    // 初始化tableView
    [self setupTableView];
}

-(void)setupTableView {
    // 设置tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc] init];
    [self.view addSubview:self.tableView];
    // 3.网络获取数据
    
    // 下拉刷新
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        // 3.1 获取活动数据
        self.page = 0;
        [self updateTeacherDatePage:0 getCount:6];
    }];
    [self.tableView.mj_header beginRefreshing];
    
    // 上拉加载
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        // 获取活动数据
        self.page++;
        [self updateTeacherDatePage:self.page getCount:6];
    }];
}

- (void)updateMenuDate {
    self.sortArray = @[@"按预约时间排序",@"按预约状态排序"];
    [self.menu reloadData];
    [self.menu selectDefalutIndexPath];
    
    // 可以添加如下方法,默认选择下拉类型
    //    DOPIndexPath *indexpath = [[DOPIndexPath alloc] initWithColumn:1 row:self.activityType];
    //    [self.menu selectIndexPath:indexpath];
}

#pragma mark - 网络获取数据
// 获取活动数据
-(void)updateTeacherDatePage:(NSInteger)page getCount:(NSInteger)count {
    // 判断是否(下拉刷新)
    if (page == 0) {
        // 下拉刷新,则清空数组
        self.dataArray = [[NSMutableArray alloc] init];
    }
    
    // 初始化参数
    NSDictionary *paramters = @{@"s_id": [UserInfoModel shareInstance].stu_id,@"token": [UserInfoModel shareInstance].token, @"page": @(page), @"count": @(count), @"teacher_sort": @(self.sort)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType URLString: @"SDSY/getTeachering" parameters: paramters success:^(NSDictionary *respondDictionary) {
        // 1.储存数据
        for (NSDictionary *dic in respondDictionary[@"Teachers"]) {
            OrderedTeacherModel *model = [[OrderedTeacherModel alloc] initWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        [self.tableView reloadData];
        // 2.检查是否正常加载
        if ([respondDictionary[@"Teachers"] count] < count) {
            // 没有更多数据
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            // 正常结束刷新
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        
        [self updateFailed];
    }];
}

-(void)updateOrderTeacherWithModel:(TeacherOrderTimeModel *)model{
    [SVProgressHUD show];
    // 请求参数
    NSDictionary *parameters = @{@"s_id": [UserInfoModel shareInstance].stu_id, @"yy_id": @(model.yyid), @"code": @(model.code), @"type": @(1)};
    
    [[NetworkTool shareInstance] requireMethodType:POSTType
                                         URLString:@"SDSY/appointTeacher"
                                        parameters:parameters
                                           success:^(NSDictionary *respondDictionary) {
                                               [SVProgressHUD dismiss];
                                               NSInteger code = [respondDictionary[@"code"] intValue];
                                               
                                               
                                               if(code == 3) {
                                                   // 取消预约成功
                                                   [SVProgressHUD showSuccessWithStatus:@"取消预约导师成功"];
                                                   [self.dataArray removeObject:model];
                                                   [self.tableView reloadData];
                                               }else{
                                                   [SVProgressHUD showfailed];
                                               }
                                           } failure:^(NSError *error) {
                                               
                                               [SVProgressHUD showErrorWithStatus:@"预约失败"];
                                           }];
}

// 根据导师+时间评价
-(void)evalutionTeacherWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid{
    EvalutionTeacherController *controller = [[EvalutionTeacherController alloc] init];
    controller.yyid = yyid;
    controller.teacherID = teacherID;
    [self.navigationController pushViewController:controller animated:true];
}

// 查看已评价的详情
-(void)viewEvalutionWithTeacherID:(NSString *)teacherID yyid:(NSInteger)yyid{
    ViewEvalutionTeacherController *controller = [[ViewEvalutionTeacherController alloc] init];
    controller.yyid = yyid;
    controller.teacherID = teacherID;
    [self.navigationController pushViewController:controller animated:true];
}

// 如果请求失败,HUD显示错误,tableVIew刷新空数据,foot提示没有数据
- (void)updateFailed {
    [SVProgressHUD showfailed];
    [self.tableView.mj_footer endRefreshingWithNoMoreData];
    [self.tableView.mj_header endRefreshing];
    [self.tableView reloadData];
}


#pragma mark - tableView回调函数
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return self.menu.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return self.menu;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [tableView cellHeightForIndexPath:indexPath cellContentViewWidth:SCREEN_WIDTH tableView:tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    OrderedTeacherCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELLID"];
    if (cell==nil) {
        cell = [[OrderedTeacherCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"CELLID"];
    }
    
    // 取出模型
    OrderedTeacherModel *model = self.dataArray[indexPath.row];
    
    // 初始化cell数据
    cell.model = model;
    
    [cell setCallBack:^(OrderedTeacherModel *model) {
        // 判断评价导师或是预约\取消导师
        if(model.stauts == 3 || model.stauts == 4) {
            // 预约(3)或取消预约(4)
            [self updateOrderTeacherWithModel:model];
        } else if (model.stauts == 2) {
            // 评价导师
            [self evalutionTeacherWithTeacherID:model.tea_id yyid:model.yyid];
        } else if (model.stauts == 1) {
            // 查看评价
            [self viewEvalutionWithTeacherID:model.tea_id yyid:model.yyid];
        }
    }];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    
    // 取出cell
    OrderedTeacherCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    // 弹出导师详情
    TeacherInfoViewController *teacherInfoViewController = [[TeacherInfoViewController alloc] init];
    teacherInfoViewController.teacherID = cell.model.tea_id;
    [self.navigationController pushViewController:teacherInfoViewController animated:true];
}

#pragma mark DOPDropDownMenuDataSource回调方法
/**
 *  返回 menu 有多少列 ，默认1列
 */
- (NSInteger)numberOfColumnsInMenu:(DOPDropDownMenu *)menu {
    return 1;
}

/**
 *  返回 menu 第column列有多少行
 */
- (NSInteger)menu:(DOPDropDownMenu *)menu numberOfRowsInColumn:(NSInteger)column {
    if (column == 0) {
        // 排序
        return self.sortArray.count;
    }
    return 0;
}

/**
 *  返回 menu 第column列 每行title
 */
- (NSString *)menu:(DOPDropDownMenu *)menu titleForRowAtIndexPath:(DOPIndexPath *)indexPath {
    if (indexPath.column == 0) {
        // 返回排序名
        return  self.sortArray[indexPath.row];
    }
    return @"";
}

/**
 *  点击代理，点击了第column 第row 或者item项，如果 item >=0
 */
-(void)menu:(DOPDropDownMenu *)menu didSelectRowAtIndexPath:(DOPIndexPath *)indexPath {
    if (indexPath.column == 0) {
        // 如果相等,就不需要赋值,不需要下拉刷新,直接返回
        if (self.sort == indexPath.row) {
            return ;
        }
        // 排序
        self.sort = indexPath.row;
    }
    [self.tableView.mj_header beginRefreshing];
    
}

- (void)menu:(DOPDropDownMenu *)menu willShowAtColumn:(NSInteger)column{
    NSLog(@"willShowAtColumn:%d",column);
    
    // tableView不能再滚动
    self.tableView.scrollEnabled = false;
}

- (void)menu:(DOPDropDownMenu *)menu willDisappearAtColumn:(NSInteger)column {
    NSLog(@"willDisappearAtColumn:%d",column);
    
    // tableView可以再次滚动
    self.tableView.scrollEnabled = true;
}


@end
